package util.mib.tree;

import model.MibNode;
import util.common.StringUtils;

import java.util.LinkedList;
import java.util.List;

public class MibZtreeUtil {
    private static class ZtreeBean{
        public String id = "";
        public String text = "";
        public String icon = "";
        public String oid = "";
        public String name = "";
        public String value = "";
        public String title = "";
        public boolean open;
        public boolean chkDisabled = true;
        public boolean isParent;
        public boolean isChildrenLoad; // 异步会用到
        public List<ZtreeBean> children = new LinkedList<>();
        
        public static ZtreeBean GetNoneTree(String info) {
            if (info == null)
                info = "暂无详细信息...";
            ZtreeBean bean = new ZtreeBean();
            bean.text = info;
            bean.title = info;
            bean.icon = "";
            bean.chkDisabled = false;
            return bean;
        }
    }

    public static String IMAGE_DIRECTORY = "/MibAnalyserTest/static/image/";

    /**
     * 转化为描述类型 zTree
     * 一般用于解析mib库后生成树
     * @param modules
     * @return
     */
    public static Object TransferToDescriptionTree(List<MibNode> modules) {
        return doTransferToDescriptionTree(modules);
    }

    private static List<ZtreeBean> doTransferToDescriptionTree(List<MibNode> modules) {
        List<ZtreeBean> nodes = new LinkedList<>();
        int i = 0;
        for (MibNode module : modules) {
            ZtreeBean bean = new ZtreeBean();
            bean.id = module.oid + "_" + module.name;
            bean.oid = module.oid;
            bean.name = module.name;
            bean.text = getDescriptionTreeText(module);
            bean.title = getFormatModuleDescription(module);
            bean.icon = getTreeIcon(module);
            bean.children = doTransferToDescriptionTree(module.children);
            bean.isParent = module.children != null && module.children.size() > 0;
            bean.isChildrenLoad = true;
            bean.open = i==0;
            nodes.add(bean);
            i ++;
        }
        return nodes.size() > 0 ? nodes : null;
    }

    private static String getTreeIcon(MibNode node) {
        if (!StringUtils.isNullOrWhiteSpace(node.name))
        {
            if (node.name.toLowerCase().endsWith("table"))
                return IMAGE_DIRECTORY + "table.png"; // 表格类型
            if (node.name.toLowerCase().endsWith("entry"))
                return IMAGE_DIRECTORY + "list.png"; // 表格对象类型
        }
        if (node.children != null && node.children.size() > 0)
            return IMAGE_DIRECTORY + "folder.png"; // 非子项类型
        else
            return IMAGE_DIRECTORY + "edit.png"; // 叶子类型
    }

    private static String getFormatModuleDescription(MibNode node) {
        StringBuilder builder = new StringBuilder();
        if (node == null) {
            return "暂无关于该OID的描述。";
        }
        if (!StringUtils.isNullOrWhiteSpace(node.moduleName)) {
            builder.append(String.format("库：%s\n", node.moduleName));
        }
        if (!StringUtils.isNullOrWhiteSpace(node.description)) {
            builder.append(String.format("描述：%s", node.description));
        }

        String des = builder.toString();
        if (StringUtils.isNullOrWhiteSpace(des)) {
            return "暂无关于该OID的描述。";
        } else {
            return des;
        }
    }

    private static String getDescriptionTreeText(MibNode node) {
        StringBuilder text = new StringBuilder(node.oid);
        if (!StringUtils.isNullOrWhiteSpace(node.name))
        {
            text.append(String.format(" [%s]", node.name));
        }

        if (!StringUtils.isNullOrWhiteSpace(node.description))
        {
            text.append(": ").append(StringUtils.cutString(node.description, 46));
        }
        return text.toString();
    }
}
